<template>
</template>
<script>
	import permision from '@/common/js/permission.js'
	
	import { httpUrl } from '@/utils/options.js'
	export default {
		data() {
			return {
				recorderManager: null,
				innerAudioContext: null,
				file: null,

				count: 0,
			}
		},
		computed: {
			timer() {
				return this.$store.state.timer
			},
		},
		onLoad() {
			let that = this
			this.init()
		},
		methods: {
			upload() {
				uni.uploadFile({
					url: httpUrl + '/nurse/order/nurseVideo',
					// url: 'http://192.168.0.47:5002/nurse/order/nurseVideo',
					// url: 'http://192.168.1.32:5002/nurse/order/nurseVideo',
					filePath: this.file,
					name: 'file',
					formData: {
						number: this.$store.state.curItem,
					},
					header: {
						token: this.$store.state.token,
					},
					success: (res) => {
						console.log(res, '////////////////////////////')
					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({
							tilte: '上传失败~',
							icon: 'none',
							duration: 1200,
						})
						return
					},
				})
			},
			init() {
				let that = this
				this.recorderManager = uni.getRecorderManager()
				this.recorderManager.onPause(function(res) {
					that.file = null
					console.log('recorder stop' + res.tempFilePath)
					that.file = res.tempFilePath
					that.upload()
				})
				this.recorderManager.onStop(function(res) {
					that.file = null
					console.log('recorder stop' + res.tempFilePath)
					that.file = res.tempFilePath
					that.upload()
				})
				this.recorderManager.onError((errMsg) => {
					plus.nativeUI.toast(errMsg)
				})
			},
			// 停止录音
			handleStop() {
				this.count = 21
				this.recorderManager = uni.getRecorderManager()
				// if(this.recorderManager) this.recorderManager.stop();
				this.recorderManager.stop()
				this.$store.commit('clearTimer')
				this.recorderManager.stop()
			},
			// 开始录音
			async handleStartRecord() {
				this.count = 0
				this.$store.commit('clearTimer')
				let that = this
				this.init()
				// let result = 0
				// if (uni.getSystemInfoSync().platform === 'android') {
				// 	result = await permision.requestAndroidPermission('android.permission.RECORD_AUDIO')
				// } else {
				// 	result = await permision.judgeIosPermission('record')
				// }
				// if(result==-1){
				// 	this.auth = 'luyin'
				// 	this.handleFlag = true
				// 	return
				// }
				this.record()
				let timer = setInterval(() => {
					that.record()
				}, 100100)
				this.$store.commit('setTimer', timer)
			},
			record() {
				if (this.count > 20) {
					this.$store.commit('clearTimer')
					this.count = 0
					return
				}
				this.count += 1
				let options = {
					duration: 100000,
				}
				this.recorderManager.start(options)
				console.log(33333333)
			},
		},
	}
</script>